//
//  TPArticleCell.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/29.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPArticleCell.h"
#import "TPArticleListModel.h"
#import "NSDate+Tripvi.h"
@interface TPArticleCell()
@property (nonatomic,weak)TPButton *lookButton;
@property (nonatomic,weak)TPButton *pinglunButton;

@end
@implementation TPArticleCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setup];
    }
    return self;
}

-(void)setup{
    
    self.backgroundColor = [UIColor whiteColor];
    UIView *bottomView = [[UIView alloc]init];
    [self.contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.0);
        make.left.mas_equalTo(10.0f);
        make.right.mas_equalTo(-10.0);
        make.height.mas_equalTo(38.0);
    }];
    [bottomView tp_addLineTopWithColor:TP_COLOR_240];
    
    
    ViewRadius(self.image_view, 4.0);
    [self.image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10.0);
        make.width.mas_equalTo(90.0);
    }];
    
    self.title_label.textColor = TPAllBackgroundColor;
    self.title_label.numberOfLines = 2;
    self.title_label.font = fontSystemOfSize(17);
    self.detail_label.numberOfLines = 2;
    [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.image_view.mas_top);
        make.height.mas_lessThanOrEqualTo(50.0);
        make.height.mas_greaterThanOrEqualTo(30.0);
        make.left.mas_equalTo(self.image_view.mas_right).offset(10.0);;
        make.right.mas_equalTo(-10.0);
    }];
    
    self.detail_label.font = fontSystemOfSize(14);
    [self.detail_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.title_label.mas_bottom).mas_offset(5.0);
        make.height.mas_lessThanOrEqualTo(55.0);
        make.height.mas_greaterThanOrEqualTo(40.0);
        make.left.mas_equalTo(self.title_label.mas_left);
        make.right.mas_equalTo(-10.0);
        make.bottom.mas_equalTo(bottomView.mas_top).offset(-10.0);
    }];
    [self.image_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.detail_label.mas_bottom);
    }];
    
    [bottomView addSubview:self.date_label];
    [self.date_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0.0);
        make.height.mas_equalTo(bottomView);
        make.bottom.mas_equalTo(0.0);
    }];
    TPButton *lookButton = [TPButton buttonWithFrame:CGRectZero title:@"" font:fontSystemOfSize(15) color:TPBlack_999999];
    [lookButton setImage:ImageNamed(@"TP_article_eye_icon") forState:UIControlStateNormal];
    lookButton.aligmentType = TPButtonAlignmentTypeCenterLeft;
    [bottomView addSubview:lookButton];
    [lookButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(bottomView);
        make.bottom.mas_equalTo(0.0f);
         make.width.mas_lessThanOrEqualTo(90.0);
        
    }];
    _lookButton = lookButton;
    
    TPButton *pinglunButton = [TPButton buttonWithFrame:CGRectZero title:@"" font:fontSystemOfSize(15) color:TPBlack_999999];
    pinglunButton.aligmentType = TPButtonAlignmentTypeCenterLeft;
    [pinglunButton setImage:ImageNamed(@"TP_article_pinglun_icon") forState:UIControlStateNormal];
    [bottomView addSubview:pinglunButton];
    [pinglunButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(bottomView.mas_right);
        make.height.mas_equalTo(bottomView);
        make.bottom.mas_equalTo(0.0f);
        make.width.mas_lessThanOrEqualTo(90.0);
    }];
    _pinglunButton = pinglunButton;
    
    
    
}
-(void)setModel:(TPArticleListModel *)model{
    _model = model;
    
    
    NSRange r;
    NSString *s = model.article_content;
    while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        s = [s stringByReplacingCharactersInRange:r withString:@""];
    NSArray *x = [s componentsSeparatedByString:@"; }"];
    s = x.lastObject;
    s = [s stringByReplacingOccurrencesOfString:@" " withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    s = [s stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    
    
    self.title_label.text = model.article_title;
    self.detail_label.text = s;
    
    [self.image_view ym_setImageWithURL:model.article_img placeholderImage:defaultLogo];
    
    
    
    self.date_label.text = [NSDate timeWithTimeIntervalString:model.article_time];
    [self.lookButton setTitle:[NSString stringWithFormat:@"%ld", model.article_view] forState:UIControlStateNormal];
    [self.pinglunButton setTitle:[NSString stringWithFormat:@"%ld",model.article_pl] forState:UIControlStateNormal];
}
@end
